using Microsoft.AspNetCore.Mvc;
using ORS.OrderRobot.ServiceContracts;

namespace ORS.OrderRobot.WebApp.Controllers
{
    /// <summary>
    /// 初始化控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class InitController : ControllerBase
    {
        private readonly IAdminService _adminService;

        public InitController(IAdminService adminService)
        {
            _adminService = adminService;
        }

        /// <summary>
        /// 初始化管理员（仅在开发环境使用）
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>创建结果</returns>
        [HttpPost("admin")]
        public async Task<ActionResult> InitAdmin([FromBody] InitAdminRequest request)
        {
            if (string.IsNullOrEmpty(request.Username) || string.IsNullOrEmpty(request.Password))
            {
                return BadRequest(new { message = "用户名和密码不能为空" });
            }

            var result = await _adminService.CreateAdminAsync(request.Username, request.Password, "Admin");
            
            if (!result.Success)
            {
                return BadRequest(new { message = result.Message });
            }

            return Ok(new { message = "管理员创建成功", admin = result.Admin });
        }
    }

    public class InitAdminRequest
    {
        public string Username { get; set; } = string.Empty;
        public string Password { get; set; } = string.Empty;
    }
} 